<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/06/02
// +----------------------------------------------------------------------

namespace app\erp\model;

use think\Model;
use think\Db;

/**
 * 仓库→门店关联管理
 * @package app\erp\model
 */
class ErpStoreWarehouse extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_erp_store_warehouse';

    /**
     * 查询
     *
     */
    public function getStoreWarehouse($store_id)
    {
        $result = Db::table($this->table)
            ->alias('sw')
            ->leftJoin('xm_erp_warehouse w','sw.warehouse_id = w.id')
            ->where('sw.store_id','=',$store_id)
            ->order('sw.create_time desc')
            ->field('sw.*,w.id as wid,w.title,w.address,w.sort,w.code,w.type')
            ->select();
        return array_out(200, 'success',$result);
    }


    /**
     * 查询排除已选中的
     */
    public function clearUp($store_id=[],$ty=false)
    {
        if(!$store_id && !$ty){
            return array_out(-1001, 'error');
        }else{
            if($ty < 1000){
                $sql = "select id,title,`type` from xm_erp_warehouse w where not exists(select warehouse_id from xm_erp_store_warehouse sw where sw.warehouse_id=w.id and sw.store_id='".$store_id."') and type ='".$ty."'";
            }else{
                $sql = "select id,title,`type` from xm_erp_warehouse w where not exists(select warehouse_id from xm_erp_store_warehouse sw where sw.warehouse_id=w.id and sw.store_id='".$store_id."')";
            }
            $list_warehouse = $this->query($sql);
            return array_out(200, 'success',$list_warehouse);
        }

    }

    /**
     * 添加数据
     * @param store_id 	                string  门店id
     * @param warehouse_id 		        string  仓库id
     * @author dou<51779987@qq.com>
     * @return array_out(warehouse_id, message, (int)id)
     */
    public function addData($store_id, $warehouse_id){
        $data['store_id']      = $store_id;
        $data['warehouse_id']  = $warehouse_id;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
    public function addDataAll($params){
        $res = Db::name('erp_store_warehouse')->insertAll($params);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
	
	/**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }
}
